/**
 * 
 *深圳市店联电子商务有限公司
 * 
 * */
package com.ruipengkj.view.baseDataView;
/**
 * 说明：
 * 添加和修改招聘信息页面
 * @Author： 朱攀
 * @Date：2012-07-25
 */
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.Date;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.KeyStroke;

import com.ruipengkj.ApplicationContext;
import com.ruipengkj.bean.TShopJob;
import com.ruipengkj.commons.BuildSpringApplication;
import com.ruipengkj.commons.util.MatcherFormatUtil;
import com.ruipengkj.commons.util.UUID;
import com.ruipengkj.commons.util.Util;
import com.ruipengkj.component.DataPackageComponent;
import com.ruipengkj.component.JLabelJComboBoxComponent;
import com.ruipengkj.component.JLabelJTextAreaComponent;
import com.ruipengkj.component.JLabelJTextFieldComponent;
import com.ruipengkj.service.TShopJobService;
import com.ruipengkj.view.base.BaseDialog;
import com.ruipengkj.view.systemView.SelectUserPanel;

public class ShopJobAddOrModifyDialog extends BaseDialog implements ActionListener{

	private static final long serialVersionUID = 1L;
	
	private JLabelJTextFieldComponent JobID;//工作编号
	private JLabelJTextFieldComponent Name;//职位名称
	private JLabelJTextFieldComponent Num;//招聘人数
	private JLabelJComboBoxComponent Sex;//员工性别   TINYINT1为男 2为女
	private JLabelJComboBoxComponent Type;//工作性质  TINYINT1为全职 2为兼职
	private JLabelJComboBoxComponent Education;//文化程度TINYINT 10为中学 20为高中 30为中技 40为中专 50 为大专 60 为本科 70为硕士 80为博士
	private JLabelJComboBoxComponent Salary;   //薪资水平TINYINT 1为面议 2为1000-2000 3为2000-3000 4为3000-4000 5为4000-5000 6为5000-6000 7为6000以上
	private JLabelJComboBoxComponent WordYear; // 工作年限  1为 1--5是相对应的工作年限，6为5年以上
	private JLabelJTextFieldComponent Email;// 邮箱 CHAR(6)
	private DataPackageComponent HappenTime;//日期
	private JLabelJTextFieldComponent Phone;//电话
	private JLabelJTextAreaComponent Des;//描述
	private JLabelJTextFieldComponent Contact;//联系人
	
	
	
	
	private JButton sureButton;
	private JButton cancelButton;
	private TShopJobService tShopJobService;//员工操作对象
	private TShopJob tShopJob=null;     //招聘信息

	public boolean isSuccess=false;
	public ShopJobAddOrModifyDialog(JFrame jf,String title,boolean b) {
		super(jf, title, b);
		tShopJobService=BuildSpringApplication.getInstance().getTShopJobServiceImpl();
		this.setSize(400,750);
		this.setLocationRelativeTo(null);
		this.initComponents();
		this.initListener();
	}
	
	private void initComponents() {
		JPanel main_jp=new JPanel();
		BoxLayout boxLayout=new BoxLayout(main_jp, BoxLayout.Y_AXIS);
		main_jp.setLayout(boxLayout);
		main_jp.setBackground(Util.PANEL_COLOR);
		
		
		//FlowLayout flowLayout=new FlowLayout(FlowLayout.LEFT);
		JPanel jp1=new JPanel();
		JobID = new JLabelJTextFieldComponent("    职位编号：", 30);
		JobID.getText().setText(UUID.GenUUID15());  //设置默认编号
		JobID.getText().setEditable(false);   //把文本框设为只读
		Name = new JLabelJTextFieldComponent("*", "职位名称：", 30);
		Sex = new JLabelJComboBoxComponent("    性    别：", ApplicationContext.SEX_MAP(),187);  
		Num= new JLabelJTextFieldComponent(" 招聘人数：", 30);
		Education=new JLabelJComboBoxComponent(" 文化程度：",ApplicationContext.JOBEDULEVEL_MAP(),187);
		Salary=new JLabelJComboBoxComponent(" 薪资水平：",ApplicationContext.SALEVEL_MAP(),187);
		Type=new JLabelJComboBoxComponent(" 工作类型：", ApplicationContext.JOBTYPE_MAP(),187);
		WordYear= new JLabelJComboBoxComponent(" 工作年限：", ApplicationContext.WYLEVEL_MAP(),187);
		Phone = new JLabelJTextFieldComponent("     电    话：", 30);
		Email= new JLabelJTextFieldComponent("    邮    箱：", 30);
		Des= new JLabelJTextAreaComponent("     描    述：", 30,2);
		HappenTime = new DataPackageComponent(" 出生日期：", 30);
		Contact=new JLabelJTextFieldComponent("    联 系人：", 30);
		sureButton=new JButton("保存)");
		cancelButton=new JButton("取消");
		
		jp1.add(JobID);
		jp1.add(Name);
		jp1.add(Sex);
		jp1.add(Num);
		jp1.add(Education);
		jp1.add(Salary);
		jp1.add(Type);
		jp1.add(WordYear);
		jp1.add(Phone);
		jp1.add(Email);
		jp1.add(Contact);
		jp1.add(HappenTime);
		jp1.add(Des);
		jp1.add(sureButton);
		jp1.add(cancelButton);
		jp1.setOpaque(false);
		main_jp.add(jp1);
		
		
		
		
		this.add(main_jp);
	}
	
	private void initListener(){
		sureButton.addActionListener(this);
		sureButton.registerKeyboardAction(this, KeyStroke.getKeyStroke(KeyEvent.VK_S, KeyEvent.CTRL_MASK), JComponent.WHEN_IN_FOCUSED_WINDOW);
		cancelButton.addActionListener(this);
		cancelButton.registerKeyboardAction(this, KeyStroke.getKeyStroke(KeyEvent.VK_Q, KeyEvent.CTRL_MASK), JComponent.WHEN_IN_FOCUSED_WINDOW);
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==sureButton){//确定按钮
			String jobID=JobID.getText().getText().trim();//编号
			String name=Name.getText().getText().trim();  //职位名称
			String SexStr=Sex.getBox().getSelectedItem().toString();//员工性别   TINYINT1为男 2为女
			String num=Num.getText().getText().trim();  //
			String education=Education.getBox().getSelectedItem().toString(); 
			String salary=Salary.getBox().getSelectedItem().toString();
			String type=Type.getBox().getSelectedItem().toString();
			String wordYear=WordYear.getBox().getSelectedItem().toString();
			String phone=Phone.getText().getText().trim();
			String email=Email.getText().getText().trim();
			String contact=Contact.getText().getText().trim();
			String des=Des.getArea().getText().trim();
			Date htime=HappenTime.getDate();
			
			
			if(name.equals("")||name.length()>30){
				JOptionPane.showMessageDialog(null, "简历名称不能为空，且不能大于30个字");
				Name.getText().requestFocus();
				return;
			}
			if(!MatcherFormatUtil.MatcherNumberScheme(num)||num.length()>4){
				JOptionPane.showMessageDialog(null, "招聘人数只能为数字，且不能大于4位");
				Num.getText().requestFocus();
				return;
			}
			if(phone.equals("")||name.length()>11){
				JOptionPane.showMessageDialog(null, "电话必能为空");
				Phone.getText().requestFocus();
				return;
			}
			if(email.equals("")||email.length()>20){
				JOptionPane.showMessageDialog(null, "邮箱不能为空");
				Email.getText().requestFocus();
				return;
			}
			if(contact.equals("")||contact.length()>20){
				JOptionPane.showMessageDialog(null, "联系人不能为空");
				Contact.getText().requestFocus();
				return;
			}
			
			if(htime==null){
				htime=new Date();
			}
			
			if(des.length()>100){
				JOptionPane.showMessageDialog(null, "你输入的字符过长，请简化！");
				Des.requestFocus();
				return;
			}
			
			
			tShopJob =new  TShopJob();
			tShopJob.setJobID(jobID);
			tShopJob.setName(name);
			tShopJob.setSex((Byte)Sex.getKeyByVaule(SexStr));
			tShopJob.setNum(Integer.valueOf(num));
			tShopJob.setEducation((Byte)Education.getKeyByVaule(education));
			tShopJob.setSalary((Byte)Salary.getKeyByVaule(salary));
			tShopJob.setType((Byte)Type.getKeyByVaule(type));
			tShopJob.setWorkYears((Byte)WordYear.getKeyByVaule(wordYear));
			
			tShopJob.setPhone(phone);
			tShopJob.setEmail(email);
			tShopJob.setContact(contact);
			tShopJob.setDes(des);
			tShopJob.setHappenTime(htime);
			
			
			
			//tUser.getTSendCardRecords().add(tSendCardRecord);
			try {
				tShopJobService.saveOrUpdateService(tShopJob);  // 掉用增加方法
				BuildSpringApplication.getInstance().getTOpLogServiceImpl().addOpLogService("增加招聘编号为：+"+jobID);
				JOptionPane.showMessageDialog(null, "操作成功");
				SelectUserPanel s=new SelectUserPanel();
				isSuccess=true;
				this.dispose();
				 
			} catch (Exception e1) {
				e1.printStackTrace();
				JOptionPane.showMessageDialog(null, "操作失败，请重新操作");
				isSuccess=false;
				
			}
			
		}else if(e.getSource()==cancelButton){//取消按钮
			this.dispose();
		}
	}
	public void showDialog(TShopJob tShopJob) {
		this.isSuccess=false;
		if(tShopJob!=null){
			JobID.getText().setText(tShopJob.getJobID());
			Name.getText().setText(tShopJob.getName());
			Sex.getBox().setSelectedItem(ApplicationContext.SEX_MAP().get(tShopJob.getSex()));
			Num.getText().setText(tShopJob.getNum()+"");
			Education.getBox().setSelectedItem(ApplicationContext.JOBEDULEVEL_MAP().get(tShopJob.getEducation()));
			Salary.getBox().setSelectedItem(ApplicationContext.SALEVEL_MAP().get(tShopJob.getSalary()));
			Type.getBox().setSelectedItem(ApplicationContext.JOBTYPE_MAP().get(tShopJob.getType()));
			WordYear.getBox().setSelectedItem(ApplicationContext.WYLEVEL_MAP().get(tShopJob.getWorkYears()));
			Phone.getText().setText(tShopJob.getPhone());
			Email.getText().setText(tShopJob.getEmail());
			Contact.getText().setText(tShopJob.getContact());
			HappenTime.setDate(tShopJob.getHappenTime());
			Des.getArea().setText(tShopJob.getDes());
			
			
		}
		this.setVisible(true);
	}
	
	
}
